package org.usp.poli.bipt.client;

import org.usp.poli.bipt.client.chart.Chart;

import com.google.gwt.dom.client.Style.Unit;
import com.google.gwt.i18n.client.LocalizableResource.Description;
import com.google.gwt.maps.client.InfoWindowContent;
import com.google.gwt.maps.client.MapWidget;
import com.google.gwt.maps.client.Maps;
import com.google.gwt.maps.client.control.LargeMapControl;
import com.google.gwt.maps.client.geom.LatLng;
import com.google.gwt.maps.client.overlay.Marker;
import com.google.gwt.user.client.ui.DecoratorPanel;
import com.google.gwt.user.client.ui.DockLayoutPanel;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.PushButton;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.visualization.client.AbstractDataTable;
import com.google.gwt.visualization.client.DataTable;
import com.google.gwt.visualization.client.VisualizationUtils;
import com.google.gwt.visualization.client.AbstractDataTable.ColumnType;
import com.google.gwt.visualization.client.visualizations.PieChart;

public class Explore {
	private DecoratorPanel explore = new DecoratorPanel();


	public Explore(){
		VerticalPanel exploreVP = new VerticalPanel();
		exploreVP.add(getDescription());
		exploreVP.add(getStats());
		exploreVP.add(getGraphics());

		explore.add(exploreVP);
	}

	private HorizontalPanel getStats(){
		HorizontalPanel stats = new HorizontalPanel();

		VerticalPanel geographicStats = new VerticalPanel();
		VerticalPanel taxonomicStats = new VerticalPanel();
		geographicStats.setTitle("Geographic Stats");
		taxonomicStats.setTitle("Taxonomic Stats");
		stats.add(geographicStats);
		stats.add(taxonomicStats);

		return stats;

	}

	private VerticalPanel getGraphics(){


		final VerticalPanel graphics = new VerticalPanel();

		final HorizontalPanel byRegionTaxon = new HorizontalPanel();
		final HorizontalPanel byBORHostingBody = new HorizontalPanel();
		byRegionTaxon.setTitle("by Region");
		byBORHostingBody.setTitle("by Taxon");

		
		Maps.loadMapsApi("", "2", false, new Runnable() {
		      public void run() {
		        buildUi();
		      }

			private void buildUi() {
				LatLng cawkerCity = LatLng.newInstance(39.509, -98.434);

			    final MapWidget map = new MapWidget(cawkerCity, 2);
			    //map.setSize("100%", "100%");
			    // Add some controls for the zoom level
			    map.addControl(new LargeMapControl());

			    // Add a marker
			    map.addOverlay(new Marker(cawkerCity));

			    // Add an info window to highlight a point of interest
			    map.getInfoWindow().open(map.getCenter(),
			        new InfoWindowContent("World's Largest Ball of Sisal Twine"));

			    final DockLayoutPanel dock = new DockLayoutPanel(Unit.PX);
			    dock.addNorth(map, 500);
			    graphics.add(dock);
			}
		    });

		  


		Runnable onLoadCallback = new Runnable() {
			public void run() {
				// Create a pie chart visualization.
				Chart chart = new Chart("teste",createTable(),"pie");
				byRegionTaxon.add(chart.getGraphicP());
				graphics.add(byRegionTaxon);
			}
		};

		// Load the visualization api, passing the onLoadCallback to be called
		// when loading is done.
		VisualizationUtils.loadVisualizationApi(onLoadCallback, PieChart.PACKAGE);



		Runnable onLoadCallback4 = new Runnable() {
			public void run() {
				// Create a pie chart visualization.
				Chart chart = new Chart("teste2",createTable(),"pie");
				byBORHostingBody.add(chart.getGraphicP());
				graphics.add(byBORHostingBody);
			}
		};

		// Load the visualization api, passing the onLoadCallback to be called
		// when loading is done.
		VisualizationUtils.loadVisualizationApi(onLoadCallback4, PieChart.PACKAGE);
		
		
		graphics.setPixelSize(600, 1200);



		return graphics;

	}




	private VerticalPanel getDescription() {
		VerticalPanel description = new VerticalPanel();
		PushButton btnMetadata = new PushButton(); 
		Label title = new Label("Description");
		HTML html = new HTML("adajsdk aks dkjas kjasbd kabsd kjbaskdb akhsbda khsbdk absdhb as");
		description.setHorizontalAlignment(VerticalPanel.ALIGN_CENTER);
		description.add(title);
		description.setCellHorizontalAlignment(title, VerticalPanel.ALIGN_LEFT);
		description.add(html);
		btnMetadata.setText("All Metadata");
		description.add(btnMetadata);
		description.setCellHorizontalAlignment(btnMetadata, VerticalPanel.ALIGN_RIGHT);

		return description;
	}

	public void setExplore(DecoratorPanel explore) {
		this.explore = explore;
	}

	public DecoratorPanel getExplore() {
		return explore;
	}




	private static AbstractDataTable createTable() {
		DataTable data = DataTable.create();
		data.addColumn(ColumnType.STRING, "Task");
		data.addColumn(ColumnType.NUMBER, "Hours per Day");
		data.addRows(2);
		data.setValue(0, 0, "Work");
		data.setValue(0, 1, 14);
		data.setValue(1, 0, "Sleep");
		data.setValue(1, 1, 10);
		return data;
	}

	public MapWidget buildmap(double[] lat,double[] longi,int n) {
		// Open a map centered on Cawker City, KS USA
		//  LatLng cawkerCity = LatLng.newInstance(39.509, -98.434);
		//  LatLng p2 = LatLng.newInstance(70.509, -138.434);
		//  LatLng p3 = LatLng.newInstance(-39.509, 98.434);

		int i;

		final MapWidget map = new MapWidget(LatLng.newInstance(39.509, -98.434), 2); //centra o mapa nessas coordenadas
		map.setSize("100%", "100%");
		// Add some controls for the zoom level
		map.addControl(new LargeMapControl());

		for(i=0;i<n;i++){
			map.addOverlay(new Marker(LatLng.newInstance(lat[i], longi[i])));
		}

		// Add a marker
		//map.addOverlay(new Marker(p2));
		//map.addOverlay(new Marker(p3));

		return map;
	}


}
